package com.ff.xyh.access.controller.access;

import com.ff.xyh.access.service.access.AccessPermissionService;
import com.ff.xyh.common.entity.result.ResultMessage;
import com.ff.xyh.common.entity.result.ResultObject;
import com.ff.xyh.common.entity.useraccess.AccessPermission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Auther FF
 * @Date 2023年 01月 15日
 * @Time 08:02
 */

@Api(tags = "后台权限接口")
@RestController
@RequestMapping("/access/permission")
public class AccessPermissionController {

    @Autowired
    private AccessPermissionService accessPermissionService;


    @ApiOperation(value = "查询所有权限")
    @GetMapping("get/list/all")
    public ResultObject getAllAccessPermission() {
        List<AccessPermission> list = accessPermissionService.list(null);
        if (list == null || list.isEmpty()){
            return ResultObject.error().message(ResultMessage.NO_RESULT);
        } else {
            return ResultObject.ok().data("items", list);
        }
    }
    @ApiOperation(value = "查询所有权限树")
    @GetMapping("get/tree/all")
    public ResultObject getAllAccessPermissionTree() {

        List<AccessPermission> selectPermissionList = accessPermissionService.list(null);

        AccessPermission rootPermission = selectPermissionList.stream()
                .filter(permission -> permission.getPid().equals("0"))
                .collect(Collectors.toList()).get(0);
        AccessPermission finalAccessPermissionObject = AccessPermission
                .getFinalAccessPermissionObject(selectPermissionList, rootPermission);
        List<AccessPermission> resultList = new ArrayList<>();
        resultList.add(finalAccessPermissionObject);
        return ResultObject.ok().putList(resultList, selectPermissionList.size());

    }

    @ApiOperation(value = "根据id查询权限")
    @GetMapping("get/{id}")
    public ResultObject getAccessPermissionById(
            @PathVariable Integer id) {
        AccessPermission accessRole = accessPermissionService.getById(id);
        if (accessRole == null ){
            return ResultObject.error().message(ResultMessage.NO_RESULT);
        } else {
            return ResultObject.ok().data("data", accessRole);
        }
    }


    @ApiOperation(value = "根据id删除权限")
    @PostMapping("delete/{id}")
    public ResultObject deleteAccessPermission(
            @PathVariable String id) {
        if ("1".equals(id)) return ResultObject.error().message("不允许删除root权限");
        AccessPermission accessRole = accessPermissionService.getById(id);
        if (accessRole == null){
            return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT);
        } else {
            if (accessPermissionService.removeById(id)) {
                return ResultObject.ok().data("data", accessRole);
            } else {
                return ResultObject.error().message(ResultMessage.OBJECT_EXISTS_BUT_FAILED_TO_DELETE);
            }
        }
    }

    @ApiOperation(value = "添加权限")
    @PostMapping("add")
    public ResultObject addAccessPermission (
            @RequestBody AccessPermission accessPermission) {
        if (accessPermissionService.getById(accessPermission.getId()) != null){
            return ResultObject.error().message(ResultMessage.OBJECT_ALREADY_EXISTS);
        } else {
            if (accessPermissionService.save(accessPermission)) {
                return ResultObject.ok().data("data", accessPermission);
            } else {
                return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT_BUT_FAILED_TO_INSERT);
            }
        }
    }
    @ApiOperation(value = "修改权限")
    @PostMapping("modify")
    public ResultObject modifyAccessPermission (
            @RequestBody AccessPermission accessPermission) {

        if ("1".equals(accessPermission.getId())) return ResultObject.error().message("不允许修改root权限");
        if (accessPermission.getType() == 1) return ResultObject.error().message("不允许修改菜单权限");

        if (accessPermissionService.getById(accessPermission.getId()) == null){
            return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT);
        } else {
            if (accessPermissionService.updateById(accessPermission)) {
                return ResultObject.ok().data("data", accessPermission);
            } else {
                return ResultObject.error().message(ResultMessage.OBJECT_EXISTS_BUT_FAILED_TO_UPDATE);
            }
        }
    }


}
